package com.ball.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ball.service.WalletService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ball.util.FileUtil.makePath;
import static com.ball.util.WalletUtil.executeNewFlow;

@Service
@Component
@PropertySource("classpath:application.properties")
public class WalletServiceImpl implements WalletService {
    @Value("${WalletSavePath}")
    private String walletSavePath ;
    @Value("${SimpleWalletPath}")
    private String simpleWalletPath;
    /*
    //List size=3
     */
    @Override
    public List<String> generate(JSONObject json) {
        String username = json.getString("username");
        String fileName = username;
        String filePath = makePath(username, walletSavePath);
        List<String> list = new ArrayList<String>();
        list.add("cd " + filePath);
        list.add(simpleWalletPath + "/simplewallet --generate-new-wallet "+username+" --password \"\" ");
        list.add("address");
        list.add("exit");
        return executeNewFlow(list,"INFO    fy");
    }
    /*
    //List size=3
     */
    @Override
    public List<String> balance(JSONObject json) {
        System.out.println("1");

        String username = json.getString("username");
        System.out.println("2");
        String fileName = username;
        String filePath = makePath(username, walletSavePath);
        List<String> list = new ArrayList<String>();
        list.add("cd " + filePath);
        list.add(simpleWalletPath + "/simplewallet --wallet-file " + username + " --password \"\" ");
        list.add("balance");
        list.add("exit");
        System.out.println(3);
        return executeNewFlow(list,"INFO    available");
    }
    /*
    //List size=3
     */
    @Override
    public List<String> address(JSONObject json) {
        System.out.println(1);
        String username = json.getString("username");
        System.out.println(2);
        String fileName = username;
        String filePath = makePath(username, walletSavePath);
        List<String> list = new ArrayList<String>();
        list.add("cd " + filePath);
        list.add(simpleWalletPath + "/simplewallet --wallet-file " + username + " --password \"\" ");
        list.add("address");
        list.add("exit");
        System.out.println(3);
        return executeNewFlow(list,"INFO    fy");
    }
    /*
    //List size=3
     */
    @Override
    public List<String> transfer(JSONObject json) {
        String username = json.getString("username");
        String address = json.getString("address");
        String pay = json.getString("pay");
        String fileName = username;
        String filePath = makePath(username, walletSavePath);
        List<String> list = new ArrayList<String>();
        list.add("cd " + filePath);
        list.add(simpleWalletPath + "/simplewallet --wallet-file " + username + " --password \"\"");
        list.add("transfer 1 " + address + " " + pay);
        System.out.println("transfer 1 " + address + " " + pay);
        list.add("exit");
        return executeNewFlow(list,"INFO    Money successfully sent","ERROR");
    }
}
